// http://coiphim.vn/
jQuery(document).ready(function($){

	var html = $('html');

	//menu 
	function menu(){
		$('ul#menu ul.sub').removeClass('child');
		$('ul#menu li').has('ul').hover(function(){
			$(this).addClass('active').children('ul').fadeIn()
		}, function() {
			$(this).removeClass('active').children('ul').stop(true, true).css('display','none')
		})
	}menu();

	//phim xem nhieu nhat tuan
	$('div#tabs_fimhot').tabs(); 	//tab

	//message --> chon the the
	function message_h2(){
		var message_h2 = $('ul#message').find('h2'),
			message_ul = $('ul#message').find('ul');
		message_h2.click(function() {message_ul.toggle(400);}); 
		html.click(function(){message_ul.hide(400);});
		message_h2.click(function(event){event.stopPropagation();});
	}message_h2();

	//login --> thong tin user
	function user(){
		var user_h3 = $('li#user').find('h3'),
			userBox = $('li#user ul#userBox');
		user_h3.click(function() {
			 userBox.toggle(400);
			 $(this).toggleClass('active');
		}); 
		html.click(function(){userBox.hide(400);});
		user_h3.click(function(event){event.stopPropagation();});
	}user();

	// show_header --> an hien header
	function header(){
		var box_menu 	= $('header').find('div.box_menu'),
			box_header	= $('header').find('div.box_header'),
			header_menu	= $('header').find('div.header_menu');
		$('header').find('div#show_header').click(function() {
			 box_menu.slideToggle(500);
			 box_header.slideToggle(500);
			 header_menu.slideToggle(500);
			 $(this).toggleClass('active')
		})
	}header();

	// user
	function dialog(){
		var dialog 			= $('#dialog'),
			dialog_login	= $('#dialog_login'),
			opener_errorplay= $('#opener_errorplay');
		$('div.close').click(function(){
			$('a.ui-dialog-titlebar-close').click()
		});
		$('.ui-widget-overlay').click(function(){
			$('a.ui-dialog-titlebar-close').click()
		});
		$('#dialog, #dialog_login, #opener_errorplay').dialog({
			autoOpen:	false,
			'modal':	true,
			show:		{effect: 'Transfer', duration: 50},
			hide:		{effect: 'Scale', duration: 100}
		});
		dialog.dialog({width:'590px'});
		dialog_login.dialog({width:'445px'});
		opener_errorplay.dialog({width:'550px'});
		$('#opener').click(function(){dialog.dialog('open')});
		$('#opener_login').click(function(){dialog_login.dialog('open')});
		$('#error_play').click(function(){opener_errorplay.dialog('open')});
	}dialog();

	//tim kien nang cao
	function accordion(){
		var extend_content	= $('div#accordion div.extend_content'),
			extend_title	= $('div#accordion div.extend_title'),
			inputString		= $('div.box_search').find('input#inputString');
		extend_title.click(function() {
			extend_content.toggle(400);
			$(this).toggleClass('active');
		});
		inputString.click(function(){extend_content.show(400);extend_title.addClass('active')});
		html.click(function(){extend_content.hide(500);extend_title.removeClass('active')});
		$('div.header_search').click(function(event){event.stopPropagation()})
	}accordion();

	//phim hot home
	function showsimg(){
		var w_showsimg  = $(document).width(),
			ul_showsimg = $('ul#showsimg'),
			foo_prev 	= $('div.pagination_foo a#foo_prev'),
			foo_next 	= $('div.pagination_foo a#foo_next');
		ul_showsimg.find('li').width(w_showsimg);
		ul_showsimg.carouFredSel({
			items:	1,
			scroll:	{pauseOnHover:'resume',fx:'crossfade',easing:'linear',duration:1500,timeoutDuration:2000,delay:2500},
			prev: 	foo_prev,
			next:	foo_next,
		}).parent().css('margin','auto')
	}showsimg();

	function caroulable(){
		for(y=1; y<=8; y++) {
			var carou_pag	= $('div.caroulable div#carou_pag'+y),
				carou_prev	= $('div.caroulable a#carou_prev'+y),
				carou_next	= $('div.caroulable a#carou_next'+y);
			if(y==5 || y==8){
				$('div.caroulable ul#caroulable'+y).carouFredSel({
					items:		3,
					auto:		false,
					scroll:		{fx:'crossfade',easing:'linear'/*,duration:300,timeoutDuration:1500,delay:1800*/},
					pagination:	carou_pag,
					prev:		carou_prev,
					next:		carou_next
				})
			}else{
				$('div.caroulable ul#caroulable'+y).carouFredSel({
					items:		5,
					auto:		false,
					scroll:		{easing:'linear',duration:650,timeoutDuration:1500,delay:1800},
					pagination:	carou_pag,
					prev:		carou_prev,
					next:		carou_next
				})
			}
		};
		var carou_prev9	= $('div.caroulable a#carou_prev9'),
			carou_next9	= $('div.caroulable a#carou_next9');
		$('div.caroulable ul#caroulable9').carouFredSel({
			items:		4,
			scroll:		{pauseOnHover:'resume',easing:'linear',duration:1200},
			prev:		carou_prev9,
			next:		carou_next9
		})
	}caroulable();

	//movie_set --> play
	function box_sever(){
		for(i=1; i<5; i++) {
			var movie_fim 	= $('div.box_sever ul#movie_fim'+i),
				a_prev		= $('div.box_sever a#prev'+i),
				a_next		= $('div.box_sever a#next'+i);
			movie_fim.carouFredSel({
				circular:	false,
				infinite:	false,
				auto:		false,
				cookie:		true,
				prev:		a_prev,
				next:		a_next
			})
		}
	}box_sever();
	
	// frame play
	function box_play(){
		var play_info	= $('div.box_play div.play_info'),
			play_frame	= $('div.box_play div.play_frame');
			//info_conter	= $('div.play div.info_conter');
		$('div.box_play span.close_play').click(function(e){
			play_info.hide();
			//info_conter.hide();
			play_frame.show('fast');
		});
		/*var show_play_info 	= $('div.box_task span#show_play_info'),
			info_conter 	= $('div.play div.info_conter');
		show_play_info.click(function() {
			 info_conter.slideToggle(500);
			 $(this).toggleClass('active');
		});*/ 
	}box_play();

	//loading img 
	/*var loaders = [{
		width: 100,
		height: 100,
		stepsPerFrame: 4,
		trailLength: 1,
		pointDistance: .01,
		fps: 25,
		fillColor: '#FF7B24',
		setup: function() {this._.lineWidth = 10;},
		step: function(point, i, f) {
			var progress = point.progress,
				degAngle = 360 * progress,
				angle = Math.PI/180 * degAngle,
				angleB = Math.PI/180 * (degAngle - 180),
				size = i*5;
			this._.fillRect(
				Math.cos(angle) * 25 + (50-size/2),
				Math.sin(angle) * 15 + (50-size/2),
				size,
				size
			);
			this._.fillStyle = '#63D3FF';
			this._.fillRect(
				Math.cos(angleB) * 15 + (50-size/2),
				Math.sin(angleB) * 25 + (50-size/2),
				size,
				size
			);
			if (point.progress == 1) {
				this._.globalAlpha = f < .5 ? 1-f : f;
				this._.fillStyle = '#EEE';
				this._.beginPath();
				this._.arc(50, 50, 5, 0, 360, 0);
				this._.closePath();
				this._.fill();
			}
		},
		path: [['line', 40, 10, 60, 90]]
	}];
	var d, a, container = document.getElementById('loading');
	for (var i = -1, l = loaders.length; ++i < l;) {
		d = document.createElement('div');
		d.className = 'loading_img';
		a = new Sonic(loaders[i]);
		d.appendChild(a.canvas);
		container.appendChild(d);
		a.play();
	};*/
	//end loading img 

	//box_facebook --> fim_left
	/*jQuery('div.box_facebook').hover(function(){
		var current = $(this);
		timerhover = setTimeout(function(){
			current.stop().animate({'left':'0'},
			{queue:false,duration:500}
		);}, 100);
		}, function(){
			clearTimeout(timerhover);
			var current = $(this);
		current.stop().animate({'left': '-289px'}, {queue:false,duration:800})
	});*/

	/*bottom-to-top*/
	function bottom_top(){
		(function(jq) {
			jq.autoScroll=function(ops){
				ops = ops||{};
				ops.styleClass=ops.styleClass||'bottom-to-top';
				var t=jq('<div class="'+ops.styleClass+'"></div>'),d=jq(ops.target||document);
				jq(ops.container||'body').append(t);
				t.click(function(){jq('html,body').animate({scrollTop: 0}, ops.scrollDuration || 1000);});
				d.scroll(function(){var sv = d.scrollTop();if(sv<10){t.clearQueue().fadeOut(ops.hideDuration||200);return;}t.css('display','').clearQueue().animate({opacity:0.8},ops.showDuration||500);});
			};
		})(jQuery);
		$.autoScroll({
			scrollDuration: 2000,
			showDuration: 	500,
			hideDuration: 	300
		})
	}bottom_top();

	//Scrollbar tab_home & info_play
	$('div#tabs_fimhot ul.right, div#Scrollbar_fim, span#Scrollbar_info').mCustomScrollbar({
		scrollButtons:		{enable:true},
		autoHideScrollbar:	true,
		advanced:			{updateOnContentResize: true}
	});

});//jQuery
//load img 
$('img.thumbnail:below-the-fold').lazyload()
$(window).bind('load', function(){
	var timeout = setTimeout(function(){$('img.thumbnail').trigger('sporty')}, 5000)
});

//load img 
